world <-map_data("world") %>%filter(region !="Antarctica")
Cleaning and processing of data, outputing top 5 rows
data <-read.csv("/private/test.csv")head(data)
X Country Fertility Population Test CountryCode
1 1 Niger 6.82 24785.587 NER NER
2 2 Somalia 6.312 16801.170 SOM SOM
3 3 Chad 6.255 16910.218 TCD TCD
4 4 Democratic Republic of the Congo 6.156 94374.379 COD COD
5 5 Central African Republic 5.978 5414.014 CAF CAF
6 6 Mali 5.956 21561.299 MLI MLI
data <- data %>%mutate(Fertility =as.numeric(as.character(Fertility)))
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Fertility = as.numeric(as.character(Fertility))`.
Caused by warning:
! NAs introduced by coercion
head(data)
X Country Fertility Population Test CountryCode
1 1 Niger 6.820 24785.587 NER NER
2 2 Somalia 6.312 16801.170 SOM SOM
3 3 Chad 6.255 16910.218 TCD TCD
4 4 Democratic Republic of the Congo 6.156 94374.379 COD COD
5 5 Central African Republic 5.978 5414.014 CAF CAF
6 6 Mali 5.956 21561.299 MLI MLI
Baseline code for the Map
#| include = FALSE#| # Set figure dimensions#| fig.width=7#| fig.height=5# Display all Brewer palettesdisplay.brewer.all()
# Filter Gapminder data and join with world map data mapa_animado_3 <- data %>%right_join(world, by =c(Country ="region")) %>%ggplot(aes(long, lat, group = group, fill =`Fertility`)) +geom_polygon(color ="white", size =0.01) +theme_void() +scale_fill_distiller(palette ="Spectral", name ="Fertility Rate") +# Use a continuous palettelabs(title ="Population & Fertility Rate", subtitle ="year: 2022") +theme(plot.title =element_text(size =12, hjust =0.5),plot.subtitle =element_text(size =10, hjust =0.5),plot.caption =element_text(size =8, hjust =1) ) +coord_fixed(ratio =1.3)
Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Convert ggplot object to a ggplotly objectfig_3 <-ggplotly(mapa_animado_3)fig_3
Trying to add hovertext with data and data Frame
# Assuming 'world' and 'data' are already loaded and prepared# Create a hover text column in 'data'data$hover_text <-paste("Country: ", data$Country, "<br>Fertility Rate: ", data$Fertility, "<br>Population: ", data$Population)joined_data <- data %>%right_join(world, by =c(Country ="region")) # Proceed with your plottingmapa_animado_3 <- joined_data %>%ggplot(aes(x = long, y = lat, group = group, fill = Fertility, text = hover_text)) +# Added 'text' aesthetic for custom hover infogeom_polygon(color ="white", size =0.01) +theme_void() +scale_fill_distiller(palette ="Spectral", name ="Fertility Rate") +# Use a continuous palettelabs(title ="Population & Fertility Rate", subtitle ="year: 2022") +theme(plot.title =element_text(size =12, hjust =0.5),plot.subtitle =element_text(size =10, hjust =0.5),plot.caption =element_text(size =8, hjust =1) ) +coord_fixed(ratio =1.3)# Convert ggplot object to a ggplotly object with hoverinfofig_3 <-ggplotly(mapa_animado_3, tooltip ="text") # Specify 'text' to use the 'text' aesthetic for the tooltipfig_3
displaying my joined_data into a dataframe and remove repeated country names
joined_data <- data %>%right_join(world, by =c(Country ="region")) %>%distinct(Country, .keep_all =TRUE)datatable(joined_data, options =list(autoWidth =TRUE))
Trying out Animation element to my map
library(shiny)
Attaching package: 'shiny'
The following objects are masked from 'package:DT':
dataTableOutput, renderDataTable
The following object is masked from 'package:crosstalk':
getDefaultReactiveDomain
library(plotly)library(ggplot2)library(dplyr)# Data preparation# Data preparation data <-read.csv("/private/test.csv") # Adjust path as necessary data <- data %>%mutate(Fertility =as.numeric(as.character(Fertility)),hover_text =paste("Country: ", Country, "<br>Fertility Rate: ", Fertility, "<br>Population: ", Population)) # Add 'hover_text' here after 'data' is defined
Warning: There was 1 warning in `mutate()`.
ℹ In argument: `Fertility = as.numeric(as.character(Fertility))`.
Caused by warning:
! NAs introduced by coercion
# UI definition world <-map_data("world") %>%filter(region !="Antarctica") joined_data <- data %>%right_join(world, by =c(Country ="region"))ui <-fluidPage(titlePanel("Interactive World Map and Data Table"),actionButton("toggle_fertility", "Countries below fertility rate "),plotlyOutput("worldMap"))server <-function(input, output, session) {# Reactive value to track the toggle state show_filtered <-reactiveVal(FALSE)# Observe the toggle button clickobserveEvent(input$toggle_fertility, {# Toggle the state current_state <-show_filtered()show_filtered(!current_state) }) output$worldMap <-renderPlotly({# Conditional color mapping based on 'show_filtered()' state color_mapping <-if(show_filtered()) {aes(fill =ifelse(Fertility <2.1, Fertility, NA)) } else {aes(fill = Fertility) } mapa_animado_3 <- joined_data %>%ggplot(aes(x = long, y = lat, group = group, text = hover_text)) +geom_polygon(color ="white", size =0.01, color_mapping) +scale_fill_distiller(palette ="Spectral", name ="Fertility Rate",na.value ="grey", limits =range(joined_data$Fertility, na.rm =TRUE)) +theme_void() +labs(title ="Population & Fertility Rate", subtitle ="year: 2022") +theme(plot.title =element_text(size =12, hjust =0.5),plot.subtitle =element_text(size =10, hjust =0.5),plot.caption =element_text(size =8, hjust =1) ) +coord_fixed(ratio =1.3)ggplotly(mapa_animado_3, tooltip ="text") })}shinyApp(ui, server)
Shiny applications not supported in static R Markdown documents